﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using bmapi.SQLServerAccess;
using Newtonsoft.Json.Linq;
using static bmapi.Models.ShowModel;

namespace bmapi.Controllers
{
    public class loginController : PageController
    {
        // GET: api/Page/5
        /// <summary>
        /// 用户登录
        /// </summary>  
        /// <param name="name">用户名或者手机号</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        [HttpPost, Route("login")]
        public HttpResponseMessage Login(dynamic obj)
        {            
            try
            {
                int loginid = Convert.ToInt32(obj.name);
                HttpContext context = HttpContext.Current;
                SQLCommand sc = new SQLCommand("select * from User_tb where login_id=@name", "@name",loginid);

                DataTable dt = sc.ExecuteScalars();
                if (dt == null)
                    return AssistFun.toJson(new RespondModel(201, "当前工号" + obj.name.ToString() + "不存在"));
                List<User> currUser = AssistFun.DataTableToList<User>(dt);
                if (currUser[0].state == 5)
                    return AssistFun.toJson(new RespondModel(201, "该用户已经离职"));
                if (currUser[0].password != obj.password.ToString())
                    return AssistFun.toJson(new RespondModel(201, "用户密码输入错误"));
                else
                {
                    //生成 token 
                    string token = CreateToken(currUser[0].login_id);
                    return AssistFun.toJson(new LoginData(200, "成功", new LoginToken(currUser[0].power, token)));
                }
            }
            catch (Exception e)
            {
                return AssistFun.toJson(new RespondModel(201, "工号" + obj.name.ToString() + "不存在,请输入正确工号"));
            }

        }
        string linee = "";
        [HttpGet, Route("menu")]
        public HttpResponseMessage RolePage()
        {
            linee = "";
            using (StreamReader sr = new StreamReader(GetJsonPath(), Encoding.UTF8))
            {
                linee = sr.ReadToEnd();
            }
            JObject json = JObject.Parse(linee);
            string menu = json["menu"].ToString();
            return new HttpResponseMessage { Content = new StringContent(menu, Encoding.GetEncoding("UTF-8"), "application/json") };
        }

        [HttpGet, Route("info")]
        public HttpResponseMessage Info()
        {
            using (StreamReader sr = new StreamReader(GetJsonPath(), Encoding.UTF8))
            {
                linee = sr.ReadToEnd();
            }
            JObject json = JObject.Parse(linee);
            string info = json["info"].ToString();
            return new HttpResponseMessage { Content = new StringContent(info, Encoding.GetEncoding("UTF-8"), "application/json") };
        }
        [HttpGet, Route("all")]
        public HttpResponseMessage all()
        {
            using (StreamReader sr = new StreamReader(GetJsonPath(), Encoding.UTF8))
            {
                linee = sr.ReadToEnd();
            }
            JObject json = JObject.Parse(linee);
            string all = json["all"].ToString();
            return new HttpResponseMessage { Content = new StringContent(all, Encoding.GetEncoding("UTF-8"), "application/json") };
        }

        private string GetJsonPath()
        {
            string path = "";
            User user = GetUserByLoginID(GetCrrentIDByToken());
            if (user == null)
                return path;
            else
                return path = HttpContext.Current.Server.MapPath("/HelpFile") + "\\menu" + user.power + ".json";
        }
    }
}
